﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using JibberJabber.PartDesigners;

namespace JibberJabber
{
    public static class SaveEverything
    {
        public static string Save()
        {
            StringBuilder xmlout = new StringBuilder();
            xmlout.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            addXmlRecursive(xmlout, Program.Root);
            return xmlout.ToString();
        }

        public static XmlTextReader SaveToXmlTextReader()
        {
            return new XmlTextReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(
                Save())));
        }

        private static void addXmlRecursive(StringBuilder xmlout, PartDesigner tag)
        {
            xmlout.AppendLine(tag.StartXml);

            lock (tag.Children)
                foreach (PartDesigner pd in tag.Children)
                    addXmlRecursive(xmlout, pd);

            xmlout.AppendLine(tag.EndXml);
        }
    }
}
